package com.xt.base.wireless.server.http;

import org.apache.commons.codec.binary.Base64;

/**
 * Author:杨洪波
 * Date:2014-1-21
 * 本应用个性加密
 */
public class Encryption {
	
	public static String e(String e){
		String s = null;
		try{
			s = Base64.encodeBase64String(e.getBytes("UTF-8"));
			if(s.length()>3){
				char[] chars = s.toCharArray();
				char c1 = chars[chars.length-1];
				char c2 = chars[0];
				chars[chars.length-1]=c2;
				chars[0]=c1;
				
				int idx =  chars.length/2-1;
				c1 = chars[idx];
				c2 = chars[chars.length-2];
				chars[idx]=c2;
				chars[chars.length-2]=c1;
				
				idx = chars.length/2+1;
				c1 = chars[idx];
				c2 = chars[chars.length-3];
				chars[idx]=c2;
				chars[chars.length-3]=c1;
				
				s = new String(chars);
			}
		}catch(Exception e1){
			e1.printStackTrace();
		}
		return s;
	}

	public static String d(String s){
		try{
			if(s.length()>3){
				char[] chars = s.toCharArray();
				char c1,c2;
				int idx;
				
				idx = chars.length/2+1;
				c1 = chars[idx];
				c2 = chars[chars.length-3];
				chars[idx]=c2;
				chars[chars.length-3]=c1;
				
				idx =  chars.length/2-1;
				c1 = chars[idx];
				c2 = chars[chars.length-2];
				chars[idx]=c2;
				chars[chars.length-2]=c1;
				
				c1 = chars[chars.length-1];
				c2 = chars[0];
				chars[chars.length-1]=c2;
				chars[0]=c1;
				
				s = new String(chars);
			}
			s = new String(Base64.decodeBase64(s),"UTF-8");
		}catch(Exception e1){
			e1.printStackTrace();
		}
		return s;
	}
	public static String d2(String s){
		try{
			if(s.length()>3){
				char[] chars = s.toCharArray();
				char c1,c2;
				int idx;
				
				idx = chars.length/2+1;
				c1 = chars[idx];
				c2 = chars[chars.length-3];
				chars[idx]=c2;
				chars[chars.length-3]=c1;
				
				idx =  chars.length/2-1;
				c1 = chars[idx];
				c2 = chars[chars.length-2];
				chars[idx]=c2;
				chars[chars.length-2]=c1;
				
				c1 = chars[chars.length-1];
				c2 = chars[0];
				chars[chars.length-1]=c2;
				chars[0]=c1;
				
				s = new String(chars);
			}
			s = new String(Base64.decodeBase64(s.getBytes()),"UTF-8");
		}catch(Exception e){
			e.printStackTrace();
		}
		return s;
	}
}
